In [8]:
import string
import copy
import scipy
import Tkinter, tkFileDialog
import numpy as np
import pandas as pd
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
import os
import sys
import datetime
import matplotlib.units
import re
from numba import jit,int32
import time
from PIL import Image
import pims
import glob
sys.path.append(os.path.abspath("C:\Users\Scherer Lab E\Documents\GitHub\Python_Data_Analysis"))
import common_functions
In [9]:
'''Import the Matlab Gui Data'''
data_dir="C:\Users\Scherer Lab E\Downloads\TrackingGUI_and_associated_files_20July2014 My Version"
os.chdir(data_dir)
file_list_proc = glob.glob('Mov_012014*pre-linking_processed.mat')
data_list_proc = [common_functions.import_matlab_gui(i) for i in file_list_proc]
data_list_proc = [common_functions.matlab_gui_to_data_frame(i) for i in data_list_proc]
Loading Mov_01201403_pre-linking_processed.mat
Loading Mov_01201404_pre-linking_processed.mat
Loading Mov_01201405_pre-linking_processed.mat
Loading Mov_01201406_pre-linking_processed.mat
Loading Mov_01201407_pre-linking_processed.mat
Loading Mov_01201408_pre-linking_processed.mat
In [10]:
os.chdir("J:\Pat's Projects\Dynamical Phase Transition\Exp01201401\Half over Nanoplate")
In [11]:
folder_list = glob.glob("Mov_012*L=?\\")
In [14]:
'''Do y-flip of position data and add polar coordinates'''
transformed_data_list_proc = []
for df in data_list_proc:
    new_flip = common_functions.y_axis_flip(df, 390)
    temp_fit_params = common_functions.least_sq_fit_circle(new_flip)
    common_functions.polar_coor_data_frame(new_flip, temp_fit_params[0], temp_fit_params[1])
    transformed_data_list_proc.append(new_flip)
In [102]:
def find_frames_with_no_particles_in_theta_region(df, theta_limits, buffer_time=1):
    """Finds frames where there are no particles in a defined theta region for 
    time +/- the buffer time
    
    :params df: The DataFrame with all the particle trajectories.
    :params theta_limits: List of length 2 that defines the lower and upper
    bounds of theta that there could be no particles.
    :params buffer_time: the number of frames before and after finding a frame
    with no particles must there also be no particles to be counted."""
    
    df_theta = df.query('@theta_limits[0] < theta < @theta_limits[1]')
    unique_frames = df_theta.drop_duplicates(subset='frame')
    
    low_frames = unique_frames[unique_frames.frame.shift(-1) - unique_frames.frame > 1+2*buffer_time]
    high_frames = unique_frames[unique_frames.frame - unique_frames.frame.shift(1) > 1+2*buffer_time]
    zipped_frames = zip(low_frames.frame, high_frames.frame)
    frame_list = []
    for low, high in zipped_frames:
        frame_list += range(int(low)+1+buffer_time, int(high)-buffer_time)
    return frame_list
    

def background_subtraction_and_save_frame_subset(df, theta_limits, buffer_time, folder):
    """Used with "find_frames_with_no_particles_in_theta_region" to find a 
    background image using only specific frames. This background is subtracted
    from each raw data image and saved in a new folder.
    
    :params df: The DataFrame with all the particle trajectories.
    :params theta_limits: List of length 2 that defines the lower and upper
    bounds of theta that there could be no particles.
    :params buffer_time: the number of frames before and after finding a frame
    with no particles must there also be no particles to be counted.
    :param folder: The path where the raw data is. Expects a tif image sequence
    that is in order when glob is called"""
    
    image_seq = pims.ImageSequence(folder+'*.tif')
    frames = find_frames_with_no_particles_in_theta_region(df, theta_limits, buffer_time)
    median = np.median(image_seq[frames], axis=0)
    global_min = abs(np.min(image_seq - median))
    try:
        os.mkdir(folder[:-1]+' Processed_Cherry_Picked')
    except:
        pass
    for num, img in enumerate(image_seq):
        new_img = img-median
        new_img = global_min + new_img
        pil_img = Image.fromarray(new_img)
        pil_img.save(folder[:-1]+' Processed_Cherry_Picked\\'+str(10000+num)+".tif")
    return median
In [103]:
for num, folder in enumerate(folder_list):
    print folder
    median = background_subtraction_and_save_frame_subset(transformed_data_list_proc[num], theta_limits=[240,300], buffer_time=1, folder=folder)
    plt.imshow(median, cmap='gray')
    plt.show()
Mov_01201403 - L=1\
Mov_01201404 - L=2\
Mov_01201405 - L=3\
Mov_01201406 - L=4\
Mov_01201407 - L=5\
In [104]:
median = background_subtraction_and_save_frame_subset(transformed_data_list_proc[-1], theta_limits=[250,300], buffer_time=1, folder=glob.glob("Mov_012*L=??\\")[0])
plt.imshow(median, cmap='gray')
plt.show()
In [ ]: